<?php
class Student extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('score_model');
		$this->load->model('student_model');
		$this->load->helper('url_helper');
		$this->load->library('session');
	}

	public function index()
	{
		$this->load->view('public/head');
		$this->load->view('student/index');
		$this->load->view('public/foot');
	}

	public function create()
	{
		//$data['rule_id'] = $rule_id;
		$test_json = file_get_contents('resource/js/test.json');
		// 把JSON字符串转成PHP数组
		$test_array = json_decode($test_json, true);
		$show_data['rule'] = $test_array;
		//var_dump($data);
		if(isset($_SESSION['user']))
			echo '欢迎回来'.$_SESSION['user'];
		$this->load->library('form_validation');
		// $this->form_validation->set_rules('parm_1', 'Parm_1', 'required');
		// $this->form_validation->set_rules('parm_2', 'Parm_2', 'required');
		$this->form_validation->set_rules('parm1_1', 'Parm1_1', 'required');
		if ($this->form_validation->run() === FALSE)
		{
			$this->load->view('public/head');
			$this->load->view('student/create_view',$show_data);
			$this->load->view('public/foot');
		}
		else
		{	
			//var_dump($_FILES);
			foreach ($_FILES as $file_item)
			{
				move_uploaded_file($file_item["tmp_name"],"upload/" . $file_item["name"]);
			}
			//move_uploaded_file($_FILES["1"]["tmp_name"],"upload/" . $_FILES["1"]["name"]);
			$this->score_model->set_score();
			$score = $this->score_model->read_score();
			$student = $this->student_model->read_student($_SESSION['user']);
			$detail_array = array('nation' =>'国家','province'=>'省级','city'=>'市级',
				'first'=>'一等奖','second'=>'二等奖','third'=>'三等奖','none'=>'未获奖');
			$data['detail']= $detail_array;
			$data['score'] = $score;
			$data['student'] = $student;
			$this->load->view('public/head');
			$this->load->view('student/succeed',$data);
			$this->load->view('public/foot');
			
		}
		

	}

	public function login(){
		$id=$_POST['id'];
		$pwd=$_POST['pwd'];
		$query=$this->student_model->login($id,$pwd);
		if(empty($query))
			echo 0;
		else
		{
			$_SESSION['user']=$query['id'];
			echo 1;
		}			
	}

	public function logout(){
		unset($_SESSION['user']);
		$url= site_url('student/create');
		//Header('Location:http://[::1]/score/index.php/student/create'); 
		Header('Location:'.$url);
	}

	public function test()
	{
		$score = $this->score_model->read_score();
		$student = $this->student_model->read_student($_SESSION['user']);
		$detail_array = array('nation' =>'国家','province'=>'省级','city'=>'市级',
			'first'=>'一等奖','second'=>'二等奖','third'=>'三等奖','none'=>'未获奖');
		$data['detail']= $detail_array;
		$data['score'] = $score;
		$data['student'] = $student;
		$this->load->view('public/head');
		$this->load->view('student/succeed',$data);
		$this->load->view('public/foot');
	}
}
 ?>